import {FormRules} from "naive-ui"
import {PathRegConst, isEmpty} from 'st-common-core'
import {MenuTypeEnum, MenuObjPropNameEnum} from "@menu"

/**
 * 菜单类型为目录时，表单信息输入规则
 */
export const DirFormRules: FormRules = {
  [MenuObjPropNameEnum.NAME]: {
    required: true,
    message: '请输入菜单名称',
    trigger: 'blur'
  },
  [MenuObjPropNameEnum.ROUTE_PATH]: {
    required: true,
    validator: (rule, value) => !isEmpty(value) && PathRegConst.ROUTE_PATH_REG.test(value),
    message: '请输入正确的路由路径',
    trigger: 'blur'
  },
}

/**
 * 菜单类型为菜单时，表单信息输入规则
 */
export const MenuFormRules: FormRules = {
  [MenuObjPropNameEnum.NAME]: {
    required: true,
    message: '请输入菜单名称',
    trigger: 'blur'
  },
  [MenuObjPropNameEnum.PAGE_PATH]: {
    required: true,
    message: '请选择菜单页面',
    trigger: 'blur'
  },
  [MenuObjPropNameEnum.ROUTE_NAME]: {
    required: true,
    message: '请输入路由名称',
    trigger: 'blur'
  },
  [MenuObjPropNameEnum.ROUTE_PATH]: {
    required: true,
    validator: (rule, value) => !isEmpty(value) && PathRegConst.ROUTE_PATH_REG.test(value),
    message: '请输入正确的路由路径',
    trigger: 'blur'
  },
}

/**
 * 菜单类型为 iframe 时，表单信息输入规则
 */
export const IframeFormRules: FormRules = {
  [MenuObjPropNameEnum.NAME]: {
    required: true,
    message: '请输入菜单名称',
    trigger: 'blur'
  },
  [MenuObjPropNameEnum.LINK_PATH]: {
    required: true,
    validator: (rule, value) => !isEmpty(value) && PathRegConst.LINK_PATH_REG.test(value),
    message: '请输入正确的链接路径',
    trigger: 'blur'
  },
}

/**
 * 菜单类型为外链时，表单信息输入规则
 */
export const LinkFormRules: FormRules = {
  [MenuObjPropNameEnum.NAME]: {
    required: true,
    message: '请输入菜单名称',
    trigger: 'blur'
  },
  [MenuObjPropNameEnum.LINK_PATH]: {
    required: true,
    validator: (rule, value) => !isEmpty(value) && PathRegConst.LINK_PATH_REG.test(value),
    message: '请输入正确的链接路径',
    trigger: 'blur'
  },
}

/**
 * 菜单类型与相应菜单类型的表单信息输入规则之间的映射关系
 */
export const FormRulesMap = {
  [MenuTypeEnum.DIR]: DirFormRules,
  [MenuTypeEnum.MENU]: MenuFormRules,
  [MenuTypeEnum.IFRAME]: IframeFormRules,
  [MenuTypeEnum.LINK]: LinkFormRules
}
